package com.qfedu.hotelssm.controller;

import com.alibaba.excel.EasyExcel;
import com.qfedu.hotelssm.dto.ComplaitExportDat;
import com.qfedu.hotelssm.dto.ComplaitStateDto;
import com.qfedu.hotelssm.entity.Complait;
import com.qfedu.hotelssm.service.intf.ComplaitService;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author javaqjb
 * @date 2022/8/29
 */
@RestController
@RequestMapping("/api/complait/")
public class ComplaitController {
    @Autowired
    private ComplaitService complaitService;

    @PostMapping("save.do")
    public R save(Complait complait) {
        return complaitService.save(complait);
    }

    @GetMapping("delete.do")
    public R delete(int id) {
        return complaitService.delete(id);
    }

    @PostMapping("update.do")
    public R update(Complait complait) {
        return complaitService.update(complait);
    }

    @GetMapping("pagr.do")
    public PageBean select(String genre, int page, int limit) {
        return complaitService.selectPage(genre, page, limit);
    }


    @PostMapping("delids.do")
    public R dels(int[] ids){
        return complaitService.delIds(ids);
    }

    //导入
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return complaitService.upload(file);
    }

    //导出Excel
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        //1.设置 响应消息头 下载
        response.setHeader("content-disposition","attachment;filename=complait-"+System.currentTimeMillis()+".xlsx");
        //2.查询学员数据
        List<ComplaitExportDat> list=complaitService.all();
        //3.实现数据写出 Excel生成
        EasyExcel.write(response.getOutputStream(),ComplaitExportDat.class).sheet(System.currentTimeMillis()+"").doWrite(list);
    }

    @RequestMapping("/templateDownload.do")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //读取服务器上的模板文件 写到浏览器
        //得到模板文件的路径
        String realPath = request.getServletContext().getRealPath("/excel/投诉管理.xlsx");
        //根据文件路径 创建File 对象
        File file = new File(realPath);
        // 把File  对象转为 字节数组

        byte[] bytes = FileUtils.readFileToByteArray(file);
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("投诉管理", "utf-8");
        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");
        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();
        // 把字节数组 输出到浏览器
        outputStream.write(bytes);
        outputStream.close();
    }
    @GetMapping("sextj.do")
    public R sexTj(){
        return complaitService.sextj();
    }


}
